﻿using CefSharp.Wpf;
using CefSharp;
using Microsoft.Extensions.Hosting;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;
using Wpf.Ui.Mvvm.Contracts;

namespace TVideo
{
    public class ApplicationHostService : IHostedService
    {
        //private readonly IServiceProvider _serviceProvider;
        //private readonly IPageService _pageService;
        //private readonly IThemeService _themeService;
        //private readonly ITaskBarService _taskBarService;
        //private INavigationWindow _navigationWindow;
        private MainWindow _mainWindow;
        public ApplicationHostService(MainWindow mainWindow)
        {
            //_serviceProvider = serviceProvider;
            //_pageService = pageService;
            //_themeService = themeService;
            //_taskBarService = taskBarService;
            _mainWindow = mainWindow;
            //_navigationWindow = navigationWindow;
        }

        public async Task StartAsync(CancellationToken cancellationToken)
        {
            InitializeCef();

            await HandleActivationAsync();
        }

        public async Task StopAsync(CancellationToken cancellationToken)
        {
            await Task.CompletedTask;
        }

        private async Task HandleActivationAsync()
        {
            //await Task.CompletedTask;
            //if (Application.Current.Windows.OfType<MainWindow>().Any())
            //{
            //    //_navigationWindow = _serviceProvider.GetService(typeof(INavigationWindow)) as INavigationWindow;

            //    //_navigationWindow = App.GetService<INavigationWindow>();
            //    _navigationWindow!.ShowWindow();

            //    // NOTICE: You can set this service directly in the window 
            //    // _navigationWindow.SetPageService(_pageService);

            //    // NOTICE: In the case of this window, we navigate to the Dashboard after loading with Container.InitializeUi()
            //    // _navigationWindow.Navigate(typeof(Views.Pages.Dashboard));
            //}

            //var notifyIconManager = _serviceProvider.GetService(typeof(INotifyIconService)) as INotifyIconService;

            //if (!notifyIconManager!.IsRegistered)
            //{
            //    notifyIconManager!.SetParentWindow(_navigationWindow as Window);
            //    notifyIconManager.Register();
            //}

            //_navigationWindow!.ShowWindow();

            _mainWindow.Show();

            await Task.CompletedTask;
        }

        private void PrepareNavigation()
        {
            //_navigationService.SetPageService(_pageService);
        }

        private void InitializeCef()
        {
            var settings = new CefSettings()
            {
                CachePath = System.IO.Path.Combine(Environment.CurrentDirectory, "CefSharp\\Cache")
            };

            settings.CefCommandLineArgs.Add("--ignore-urlfetcher-cert-requests", "1");
            settings.CefCommandLineArgs.Add("--ignore-certificate-errors", "1");
            settings.CefCommandLineArgs.Add("enable-media-stream");
            settings.CefCommandLineArgs.Add("use-fake-ui-for-media-stream");
            settings.CefCommandLineArgs.Add("enable-usermedia-screen-capturing");
            settings.UserAgent = "tvideo-win-cef-chrome/107.1.4";
            
            CefSharp.Cef.Initialize(settings);
        }
    }
}
